<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="字段配置" @del="del" @newCreate="newCreate" @configCoulmn="configCoulmn" ></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" :api="apiMap.getListApi" :apiParams="apiParams" :columnSource="baseColumns" :cbFun="cbFun" @selection-change="data => tableSelectData = data"
            @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'字段配置'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" labelWidth="120px" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>


    <el-dialog title="配置生效表字段" :visible.sync="dialogColumnVisible" width="720px">
      <div v-tit="'配置生效表字段'">
        <el-transfer 
        filterable
        filter-placeholder="请输入字段名称"
        v-model="currentcolumn" 
        :data="allcolumn"
        :titles="['表字段', '生效字段']"
        :props="{
          key: 'columnname',
          label: 'columnname'
        }"
        ></el-transfer>
      </div>
    <div slot="footer" class="dialog-footer">
    <el-button @click="dialogColumnVisible = false">取 消</el-button>
    <el-button type="primary" @click="configActivecolumn">保存</el-button>
  </div>
  </el-dialog>
  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle';

export default {
  name: 'columnvalueconfig',
  data() {
    return {
      allcolumn:[],
      currentcolumn:[],
      dialogColumnVisible:false,
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        id: null,
        columnname:'',
        columndescr:'',
        requestvaluepath:'',
        convertrule:'',

      },
      options:{
        requestnode:[],
        customprocess:[],
        seriakeyrule:[]
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: '新建字段配置',
          invalid: 'newCreate'
        },
        {
          label: '删除字段配置',
          disabled: true,
          invalid: 'del'
        },
        {
          label: '配置生效表字段配置',
          invalid: 'configCoulmn'
        }
      ]
    },
    baseColumns() {
      return [
        {
          label: '字段名称',
          prop: 'columnname',
          input: 'input'
        },
         {
          label: '字段描述',
          prop: 'columndescr',
          input: 'input'
        },
        {
          label: '报文值路径',
          prop: 'requestvaluepath',
          input: 'select',
          options:this.options.requestnode
        },
        {
          label: '字段值转换规则',
          prop: 'convertrule',
          input: 'input'
        },
        {
          label: '是否为流水号',
          prop: 'isseriakey',
          input: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '是否为更新条件字段',
          prop: 'updcondition',
          input: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '是否为被更新字段',
          prop: 'updcolumn',
          input: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '覆盖值',
          prop: 'overridevalue',
          input: 'input'
        },
        {
          label: '默认值',
          prop: 'defaultvalue',
          input: 'input'
        },
        {
          label: '是否为wms行号',
          prop: 'islinenumber',
          input: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '是否被作为报文字段',
          prop: 'activerequest',
          input: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '不作为insert字段（默认为false）',
          prop: 'skipinsert',
          input: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '是否为仓库号',
          prop: 'iswhseid',
          input: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        }
      ]
    },
    formConfig() {
      return [
        {
          label: '字段名称',
          prop: 'columnname',
          type: 'input'
        },
        {
          label: '字段描述',
          prop: 'columndescr',
          type: 'input'
        },
        {
          label: '报文值路径',
          prop: 'requestvaluepath',
          type: 'select',
          options:this.options.requestnode
        },
        {
          label: '字段值转换规则',
          prop: 'convertrule',
          type: 'input'
        },
        {
          label: '是否为流水号',
          prop: 'isseriakey',
          type: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '覆盖值',
          prop: 'overridevalue',
          type: 'input'
        },
        {
          label: '默认值',
          prop: 'defaultvalue',
          type: 'input'
        },
        {
          label: 'uui填充',
          prop: 'isuuid',
          type: 'input'
        },
        {
          label: '是否为wms行号',
          prop: 'islinenumber',
          type: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '是否更新条件字段',
          prop: 'updcondition',
          type: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '是否为被更新字段',
          prop: 'updcolumn',
          type: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '是否被作为报文字段',
          prop: 'activerequest',
          type: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '不作为insert字段（默认为false）',
          prop: 'skipinsert',
          type: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        },
        {
          label: '是否为仓库号',
          prop: 'iswhseid',
          type: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        }
      ]
    },
    formRules() {
      return {
        activeclass: [
          {
            required: true,
            message: '请选择表名称',
            trigger: 'change'
          }
        ],
      }
    },
    apiMap() {
      return {
        getListApi: '/v1/ediColumnValueConfig/selectColumnValueConfigList',
        getInfoApi:'/v1/ediColumnValueConfig/selectColumnValueConfigById',
        editApi:'/v1/ediColumnValueConfig/editEdiColumnValueConfig',
        deleteApi: '/v1/ediColumnValueConfig/deleteEdiColumnValueConfig',
        addApi: '/v1/ediColumnValueConfig/addEdiColumnValueConfig'
      }
    },
    apiParams() {
      return { editableconfigid: this.$route.query.id }
    }
  },
  methods: {
    configActivecolumn(){
      this.$post('/v1/ediColumnValueConfig/saveCurrentConfig',{editableconfigid:this.$route.query.id,currentcolumn:this.currentcolumn}).then(res => {
        this.$message.success(res.returnMgr);
        this.dialogColumnVisible = false;
        this.getTableData();
      })
    },
    configCoulmn(){
      this.$post('/v1/ediColumnValueConfig/queryCurrentConfig',{editableconfigid:this.$route.query.id}).then(res => {
        this.allcolumn = res.data.map.allcolumn;
        this.currentcolumn = res.data.map.currentcolumn;
      })
      this.dialogColumnVisible= true;
    },
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)
        params.editableconfigid = this.$route.query.id;
        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(this.apiMap.editApi, params)
        } else {
          promise = this.$post(this.apiMap.addApi, params)
        }
        promise.then(res => {
          if(res.returnCode == 400){
            this.$message.error(res.returnMgr)
          }else{
            this.getTableData()
            this.$refs.detail?.resetFields()
            this.dialogModule.flag = false
          }

        })
      })

    },
    getInfoById(id) {
      this.$post(this.apiMap.getInfoApi, { 
        id
      }).then(res => {
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑字段配置`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增字段配置'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$post(this.apiMap.deleteApi, data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },

    closed() {
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
  },
  mounted() {
      this.$post('/v1/ediColumnValueConfig/requestNodeSelect',{editableconfigid:this.$route.query.id}).then(res => {
        this.options.requestnode = res.data.item;
      })

      this.$post('/v1/ediColumnValueConfig/queryserialrule',{}).then(res => {
        this.options.seriakeyrule = res.data.item;
      })
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
